﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Text;

namespace test
{
    [TestClass]
    public class UnitTest_Texttool
    {
        static string code =
            @"Shader	""Custom/Chapter5-SimpleShader"" {
				/*加好多注释*/
				//+描边
				//+阴影
				SubShader{
					Pass{
						CGPROGRAM

						// 两条非常重要的编译指令
							  #pragma vertex vert // 定点着色器
							  #pragma fragment frag // 片元着色器

						// 这一步是把顶点坐标从模型空间转换到裁剪空间
							  float4 vert(float4 v : POSITION) : SV_POSITION {
								  return mul(UNITY_MATRIX_MVP, v);
							  }

						  fixed4 frag() : SV_Target{
								  return fixed4(1.0,1.0,1.0,1.0);
							  }

						ENDCG
					}
				}
			}
			";
        static int CodeEqual(string code1, string code2)
        {
            int diff = 0;
            int seed1 = 0;
            int seed2 = 0;
            while (true)
            {
                //结束检查
                if (seed1 >= code1.Length && seed2 >= code2.Length)
                    break;

                var c1 = code1[seed1];
                var c2 = code2[seed2];

                //忽略次要字符
                while (c1 == '\r' || c1 == '\n' || c1 == '\t' || c1 == ' ')
                {
                    seed1++;
                    if (seed1 >= code1.Length)
                        break;
                    c1 = code1[seed1];
                }
                while (c2 == '\r' || c2 == '\n' || c2 == '\t' || c2 == ' ')
                {
                    seed2++;
                    if (seed2 >= code2.Length)
                        break;
                    c2 = code2[seed2];
                }

                //结束检查
                if (seed1 >= code1.Length && seed2 >= code2.Length)
                    break;

                if (c1 == c2)
                {

                }
                else
                {
                    diff++;
                }
                seed1++;
                seed2++;

                //结束检查
                if (seed1 >= code1.Length && seed2 >= code2.Length)
                    break;
            }
            diff += (code1.Length - seed1);
            diff += (code2.Length - seed2);
            return diff;
        }
        [TestMethod]
        public void Test_DirectConvect_Simple()
        {

            Console.WriteLine("===============>old");
            Console.WriteLine(code);
            var shader = MTShader.TextTool.FromCode(code);
            var text = MTShader.TextTool.FormatShader(shader);
            Console.WriteLine("===============>new");
            Console.WriteLine(text);

            var textrree = MTShader.TextTool.FormatShader(shader, true);
            Console.WriteLine("===============>tree");
            Console.WriteLine(textrree);
            Assert.IsTrue(CodeEqual(code, text) == 0);
        }
        [TestMethod]
        public void Test_DirectConvect_File()
        {
            var _code = dumptool.LoadStr("testdata\\ML_NHero_L1_T2.shader");
            Console.WriteLine("===============>old");
            Console.WriteLine(_code);
            var shader = MTShader.TextTool.FromCode(_code);
            var text = MTShader.TextTool.FormatShader(shader);
            Console.WriteLine("===============>new");
            Console.WriteLine(text);
            Assert.IsTrue(CodeEqual(_code, text) == 0);

        }

        [TestMethod]
        public void Test_Replace()
        {
            Console.WriteLine("===============>old");
            Console.WriteLine(code);
            var file = MTShader.TextTool.FromCode(code);


            //调用替换操作
            //这种节点的第一个位置就是string
            var shader = file.FindOne_ByType("shader");
            shader.children[1].text = "\"change text\"";

            var funcs = shader.FindAll_ByType("cg_func");
            foreach (var f in funcs)
            {
                f.children[1].text += "_fix";
                Console.WriteLine("got func:" + f.children[1].text);
            }

            //只需要多添加一些find 结构，就可以稳定的找到语法元素位置

            Console.WriteLine("===============>new");
            var text = MTShader.TextTool.FormatShader(file);
            Console.WriteLine(text);

        }
    }
}
